package main.java.com.silence.controller;

import com.silence.constant.AttributeConstant;
import com.silence.constant.ViewNameConstant;
import com.silence.pojo.Bill;
import com.silence.pojo.BillType;
import com.silence.pojo.User;
import com.silence.service.IBillService;
import com.silence.service.IBillTypeService;
import com.silence.vo.Pager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author: silence
 * Date: 17-6-4
 */
@Controller
public class BillController {

    private static Logger LOGGER = Logger.getLogger(BillController.class);

    @Resource
    private
    IBillService billService = null;

    @Resource
    private IBillTypeService billTypeService = null;

    /**
     * 添加一条bill
     */
    @RequestMapping(value = "/bill", method = RequestMethod.POST)
    public ModelAndView addBill(Bill bill, HttpServletRequest request, ModelAndView modelAndView) throws Exception {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute(AttributeConstant.LOGIN_USER_INFO);
        Integer ownerId = user.getId();

        if (bill != null) {

            bill.setOwner(ownerId);

            int temp = billService.add(bill);

            if (temp == 0) {
                throw new Exception("添加失败");
            }
        } else
            throw new Exception("Bill is empty");

        modelAndView.setViewName(ViewNameConstant.REDIRECT_HOME);

        return modelAndView;
    }

    /**
     * 通过id更新一条bill
     */
    @RequestMapping(value = "/bill/{billId}", method = RequestMethod.POST)
    public ModelAndView updateBill(@PathVariable Integer billId, Bill bill, HttpServletRequest request,
                                   ModelAndView modelAndView) throws Exception {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute(AttributeConstant.LOGIN_USER_INFO);

        int ownerId = billService.findById(billId).getOwner();
        if (user.getId() != ownerId) {

            throw new Exception("该用户无此权限");
        } else {

            LOGGER.info(bill);

            bill.setId(billId);
            bill.setOwner(user.getId());
            int flag = billService.update(bill);

            LOGGER.info(flag);

            if (flag != 1) {
                throw new Exception("修改失败");
            }
        }

        modelAndView.setViewName(ViewNameConstant.REDIRECT_HOME);
        return modelAndView;
    }

    /**
     * 编辑一条已存在的bill
     */
    @RequestMapping(value = "/bill_edit/{billId}", method = RequestMethod.GET)
    public ModelAndView billEdit(@PathVariable Integer billId, ModelAndView modelAndView) throws Exception {

        Bill bill;

        if (billId != null && (bill = billService.findById(billId)) != null) {

            List<BillType> billTypes = billTypeService.findAll();

            modelAndView.addObject(AttributeConstant.BILL_TYPE, billTypes);
            modelAndView.addObject(AttributeConstant.BILL_INFO, bill);
            modelAndView.setViewName(ViewNameConstant.BILL_EDIT);

        } else {
            throw new Exception("不存在该条Bill");
        }

        return modelAndView;

    }

    /**
     * 添加一条bill页面
     */
    @RequestMapping(value = "/bill", method = RequestMethod.GET)
    public String billAddPage(Model model) {

        List<BillType> billTypes = billTypeService.findAll();

        model.addAttribute(AttributeConstant.BILL_INFO, new Bill());

        model.addAttribute(AttributeConstant.BILL_TYPE, billTypes);

        return ViewNameConstant.BILL_ADD;
    }

    /**
     * 通过bill id删除一条用户数，还未做权限控制
     */
    @RequestMapping(value = "/bill/delete/{billId}", method = RequestMethod.POST)
    public int deleteBill(@PathVariable Integer billId) throws Exception {

        if (billId != null) {
            int flag = 0;
            flag = billService.deleteById(billId);
            if (flag != 1) {
                throw new Exception("删除失败");
            }
            return flag;
        }
        throw new Exception("Bill ID is null");
    }

    /** 测试分页 */
    @RequestMapping(value = "/pager/{pageIndex}", method = RequestMethod.GET)
    public String pager(@PathVariable int pageIndex, Model model) {

        int id = 1;
        Pager<Bill> billPager = billService.findAllByOwnerId(id, pageIndex);

        model.addAttribute("pager", billPager);
        return "pagetest";
    }
}
